package bb.lanxing.device.sgdevcie.xpair.viewmodel

import android.annotation.SuppressLint
import android.app.Application
import android.bluetooth.BluetoothAdapter
import bb.lanxing.device.sgdevcie.xpair.uimodel.BleDescriptionDevice
import bb.lanxing.mvvm.base.viewmodel.BaseViewModel
import bb.lanxing.util.Log
import com.nostra13.universalimageloader.utils.L
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat
import no.nordicsemi.android.support.v18.scanner.ScanCallback
import no.nordicsemi.android.support.v18.scanner.ScanResult
import no.nordicsemi.android.support.v18.scanner.ScanSettings

class SearchDeviceViewModel(application: Application) : BaseViewModel(
    application
) {
    var deviceArrivalCallback: ((BleDescriptionDevice) -> Unit)? = null
    var scanCallBack: ((Boolean) -> Unit)? = null
    private val scanCallback: SearchDeviceScanCallback = SearchDeviceScanCallback()
    private val scanner: BluetoothLeScannerCompat = BluetoothLeScannerCompat.getScanner()
    private val settings: ScanSettings = ScanSettings.Builder().setLegacy(false).setScanMode(1)
        .setUseHardwareBatchingIfSupported(true).build()
    private val tempDevice: MutableList<String> = mutableListOf()

    fun startScan() {
        try {
            tempDevice.clear()
            scanner.startScan(null, this.settings, this.scanCallback)
            scanCallBack?.invoke(true)
        } catch (e: Exception) {
            L.e(e.message)
        }
    }

    fun stopScan() {
        tempDevice.clear()
        scanner.stopScan(this.scanCallback)
    }

    public override fun onCleared() {
        scanner.stopScan(this.scanCallback)
        super.onCleared()
    }

    @SuppressLint("MissingPermission")
    inner class SearchDeviceScanCallback : ScanCallback() {
        override fun onScanFailed(param1Int: Int) {
            if (param1Int == 2) BluetoothAdapter.getDefaultAdapter().disable()
            scanCallBack?.invoke(java.lang.Boolean.FALSE)
            Log.d("SearchDeviceViewModel", "On scan failed")
        }

        override fun onScanResult(param1Int: Int, param1ScanResult: ScanResult) {
            val charSequence: CharSequence? = param1ScanResult.device.name
            var param1 = if (charSequence.isNullOrEmpty()) {
                1
            } else {
                0
            }
            if (param1 == 0 && !tempDevice.contains(param1ScanResult.device.address)) {
                val list = this@SearchDeviceViewModel.tempDevice
                var str1 = param1ScanResult.device.address
                list.add(str1)
                str1 = param1ScanResult.device.address
                val str2 = param1ScanResult.device.name
                param1 = param1ScanResult.rssi
                val bluetoothDevice = param1ScanResult.device
                var scanRecord = param1ScanResult.scanRecord
                if (scanRecord != null) {
                    val arrayOfByte = scanRecord.bytes
                } else {
                    scanRecord = null
                }
                deviceArrivalCallback?.invoke(
                    BleDescriptionDevice(
                        str1,
                        str2,
                        param1,
                        bluetoothDevice,
                        scanRecord!!.bytes,
                        null,
                        null,
                        null,
                        null,
                        null,
                        0,
                        0,
                        4064,
                        null
                    )
                )
                val stringBuilder = StringBuilder()
                stringBuilder.append("On scan device:")
                stringBuilder.append(param1ScanResult.device.name)
                stringBuilder.append(" address: ")
                stringBuilder.append(param1ScanResult.device.address)
                Log.d("SearchDeviceViewModel", stringBuilder.toString())
            }
        }
    }

    companion object {
        const val TAG: String = "SearchDeviceViewModel"
    }
}
